//手机号登录
import { View, Button, Input, Radio, Text, Form } from "@tarojs/components";
import "./index.scss";
import React, { useState, useEffect } from "react";
import Taro, { setStorageSync } from "@tarojs/taro";
import store from "../../store";
import http from "../../utils/http";
let Login = () => {
  definePageConfig({
    navigationBarTitleText: "登录/注册"
  });
  const [checked, setChecked] = useState(true);
  const [value, setValue] = useState("");
  const regPhone = /^1(3[0-9]|5[0-3,5-9]|6[0-9]|7[1-3,5-8]|8[0-9])\d{8}$/;
  const [pass, setPass] = useState(""); //密码
  let judge = () => {
    if (regPhone.test(value)) {
      if (!checked) {
        Taro.navigateTo({
          url: "./agreement",
          success: () => {
            Taro.showToast({
              title: "请先阅读并同意勾选商户入驻协议!",
              icon: "none",
              duration: 2000
            });
          }
        });
      } else {
        http({
          url: "/role/userLogin",
          method: "post",
          data: {
            phone: value,
            password: pass
          }
        }).then(res => {
          if (res.data.code == 200) {
            Taro.setStorageSync("token", res.data.data.token);
            Taro.setStorageSync("massage",res.data.data)//把后台返回的数据存在本地
            Taro.getUserProfile({
              desc: "用于完善会员资料", // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
              success: res => {
                Taro.setStorageSync("userImg", res.userInfo.avatarUrl);//把获取到的用户头像存在本地
                Taro.switchTab({
                  url: "../Group/group/group"
                });
              }
            });
          } else {
            Taro.showToast({
              title: "账号/密码错误!",
              icon: "error",
              duration: 2000
            });
          }
        });
      }
    } else {
      Taro.showToast({
        title: "请输入正确的手机号码!",
        icon: "none",
        duration: 2000
      });
    }
  };
  useEffect(() => {
    setChecked(store.s_wh.radio);
  });
  return (
    <View className="login">
      <View className="tit">
        <Text>账号登录</Text>
        <Text
          onClick={() => {
            Taro.navigateTo({
              url: "./index"
            });
          }}
          style={{ fontSize: " 20px" }}
        >
          注册账号
        </Text>
      </View>
      <View>
        <Input
          type="number"
          placeholder="请输入手机号"
          onInput={value => {
            setValue(value.detail.value);
          }}
        />
      </View>
      <View>
        <Input
          type="number"
          placeholder="请输入登录密码"
          onInput={value => {
            setPass(value.detail.value);
          }}
        />
      </View>
      <View>
        <Radio checked={checked} disabled></Radio>
        <Text
          onClick={() => {
            Taro.navigateTo({
              url: "./agreement"
            });
          }}
          style={{ color: "#27c3fd", textDecoration: "underline" }}
        >
          点击阅读商户入驻协议
        </Text>
      </View>
      <Button className="next" onClick={judge}>
        下一步
      </Button>
    </View>
  );
};
export default Login;
